package com.speedchina.permission.service.sys.impl;

import com.speedchina.framework.base.TService;
import com.speedchina.permission.base.constant.SysConstant;
import com.speedchina.permission.base.util.UserAgentUtil;
import com.speedchina.permission.dao.sys.SysLogLoginDao;
import com.speedchina.permission.domain.sys.entity.SysLogLogin;
import com.speedchina.permission.domain.sys.entity.User;
import com.speedchina.permission.service.sys.SysLogLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *  服务层实现类
 * @author Winter
 * @date 2021-06-09 16:10:43
 */
@Service
public class SysLogLoginServiceImpl extends TService<SysLogLoginDao, SysLogLogin> implements SysLogLoginService {
    @Autowired
    RedisTemplate redisTemplate;
    @Value("${speedchina.permission.jwt.expire:604800}")
    Long expire;
    /**
     * 保存登录日志
     * @description
     * @author Winter
     * @date 2021-06-09 17:09
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSysLogLogin(User userPo, String jwtToken) {
        SysLogLogin logLogin = new SysLogLogin();
        logLogin.setLoginTime(new Date());
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String ip = request.getRemoteAddr();
        Map<String, String> map = UserAgentUtil.getInfo(request);
        logLogin.setLoginSystem(map.get("system"));
        logLogin.setUserBrowser(map.get("browserName"));
        logLogin.setIp(ip);
        logLogin.setUserId(userPo.getId());
        logLogin.setLoginName(userPo.getNickname());
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime logoutTime = now.plus(expire, ChronoUnit.SECONDS);
        logLogin.setLoginTime(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()));
        logLogin.setUserName(userPo.getUsername());
        logLogin.setLogoutTime(Date.from(logoutTime.atZone(ZoneId.systemDefault()).toInstant()));
        this.saveOrUpdate4AutoGenId(logLogin);

        //将id存入redis
//        String key = SysConstant.SYS_LOG + userPo.getUsername() + "_id";
        String key = SysConstant.SYS_LOGIN_LOG + jwtToken;
        String value = logLogin.getId();
        //设置过期时间
        redisTemplate.opsForValue().set(key,value,expire, TimeUnit.SECONDS);
        return logLogin.getId()!=null;
    }
    @Override
    public boolean updateLogLogin(String token){
        if(StringUtils.isEmpty(token)){
            return true;
        }
        String key = SysConstant.SYS_LOGIN_LOG + token;
        String  id = (String) redisTemplate.opsForValue().get(key);
        if(StringUtils.isEmpty(id)){
            return true;
        }
        SysLogLogin logLogin = this.getById(id);
        if(logLogin==null){
            return true;
        }
        logLogin.setLogoutTime(new Date());
        logLogin.setId(id);
        return this.updateById(logLogin);
    }
}